/*
 * UserDaoImpl.java
 *
 * Created on September 24, 2008, 9:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.dal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.vacuela.publishmx.domains.User;
import org.springframework.transaction.annotation.Transactional;
/**
 *
 * @author nestor.carrasco
 */
public class UserDaoImpl extends SqlMapClientDaoSupport  implements UserDaoIfc {
    
    /** Creates a new instance of UserDaoImpl */
    public UserDaoImpl() {
    }

    @Transactional
    public boolean saveUpdate(User user) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        if (user.getId() == null){
            template.insert("insertUser", user);	
        }else{       
            template.update("updateUser", user);
        }
        return true;
    }

    @Transactional(readOnly=true)
    public User getUser(Integer userId) {
         SqlMapClientTemplate template = getSqlMapClientTemplate();
         User objectUser = (User) template.queryForObject("selectUserById", userId);
         return objectUser;
    }

    
    @Transactional(readOnly=true)
    public List<User> getUserList() {
         SqlMapClientTemplate template = getSqlMapClientTemplate();
        return (List<User>)template.queryForList("selectAllUsers");
        
    }

    @Transactional(readOnly=true)
    public User getByUserAndPassword(String userId, String pass) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("userId", userId);
        mapParam.put("password", pass);
        User objectUser = (User) template.queryForObject("searchByUserAndPassword", mapParam);
        return objectUser;        
       
    }

    @Transactional(readOnly=true)
    public List findUserByName(String name) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("criteria",name+"%");
        return (List<User>)template.queryForList("findUsersByName",mapParam);
       
        
    }

    @Transactional
    public boolean deleteUser(Integer userId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        template.delete("deleteUser", userId);
        return true;
    }
    
}
